#!/bin/sh
# PCP QA Test No. 1309
# Test Linux ZFS PMDA against a live ZFS pool - part B (newer stats files)
# See also qa/1532 for part A
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ $PCP_PLATFORM = linux ] || _notrun "Linux ZFS PMDA is Linux-specific (uses procfs)"
[ -d $PCP_PMDAS_DIR/zfs ] || _notrun "Linux ZFS PMDA not installed"
grep -q zfs /proc/filesystems || _notrun "ZFS filesystem unsupported by the running kernel"
which zpool > /dev/null || _notrun "zpool not found, please check your OpenZFS installation"
zpool -? 2>&1 | grep -q "make sure that the zfs-fuse daemon is running."
[ $? -eq 0 ] && _notrun "zfs-fuse is installed but test targets OpenZFS"

for file in abdstats dbufstats dnodestats vdev_mirror_stats
do
    [ -f /proc/spl/kstat/zfs/$file ] || _notrun "/proc/spl/kstat/zfs/$file is missing"
done

status=1
trap "cd $here; $sudo rm -rf $tmp.*; exit \$status" 0 1 2 3 15

if [ ! -f $PCP_PMDAS_DIR/zfs/help.pag ]
then
    # PMDA has not been Install'd ... we need the compiled help files
    # for this test
    #
    cd $PCP_PMDAS_DIR/zfs
    $sudo $PCP_BINADM_DIR/newhelp -n root -o help <help
    cd $here
fi

_filter()
{
    $here/src/sortinst \
    | sed -e '/inst /s/\[[01] /[0-or-1 /'
}

pmda=$PCP_PMDAS_DIR/zfs/pmda_zfs.so,zfs_init
pmns=$PCP_PMDAS_DIR/zfs/root
opts="-L -K clear -K add,153,$pmda -n $pmns"
export ZFS_PATH=
poolname=pmdazfs_test
root=$tmp.root
$sudo rm -f $PCP_VAR_DIR/config/pmda/153.*
$sudo rm -fr $root
mkdir $root || _fail "root in use when processing $tgz"
$sudo dd if=/dev/zero of=$root/disk.img bs=1024 count=65536 > /dev/null 2>&1
$sudo zpool create $poolname $root/disk.img
echo "== Checking metric count for ZFS ABD"
pminfo $opts -f zfs.abd |  grep '^ \+value ' | wc -l 2>&1
echo "== Checking metric count for ZFS DBUF"
pminfo $opts -f zfs.dbuf |  grep '^ \+value ' | wc -l 2>&1
echo "== Checking metric count for ZFS DNode"
pminfo $opts -f zfs.dnode |  grep '^ \+value ' | wc -l 2>&1
echo "== Checking metric count for ZFS VDev"
pminfo $opts -f zfs.vdev |  grep '^ \+value ' | wc -l 2>&1
echo "== Checking metric count for ZFS pool $poolname"
pminfo $opts -f zfs.pool | grep $poolname | wc -l 2>&1
echo && echo "== done" && echo
$sudo zpool destroy $poolname
$sudo rm $root/disk.img
# success, all done
status=0
exit
